package com.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.model.po.Food;
import com.springboot.service.FoodService;

@RefreshScope	//监听动态刷新
@RequestMapping("/food")
@RestController
public class FoodController {
	@Autowired
	FoodService foodService;
	
	@GetMapping("/{foodId}")
	public ResponseEntity getFoodById(@PathVariable int foodId) throws Exception{
		Food food = foodService.getFoodById(foodId);
		if(food == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("The requested resource was not found");
		}else {
			return ResponseEntity.ok(food);
		}
	}
	
	@GetMapping("/lists/{businessId}")
	public ResponseEntity listFoodByBusinessId(@PathVariable Integer businessId) throws Exception{
		List<Food> list = foodService.listFoodByBusinessId(businessId);
		if(list == null || list.size() == 0) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("The requested resource was not found");
		}else {
			return ResponseEntity.ok(list);
		}
	}
}
